import axios from 'axios'
import com from './com'
import vue from '../../main'
/* eslint-disable */

const baseUrl = [
  'https://server.stream.sano100.com',
  'http://192.168.31.99:2022',
  "http://192.168.1.99:2024",
  "http://localhost:2024",
  'http://zhongpeng.sano100.com'
]
const urlIndex = 1
export default {
  getUrl () {
    return baseUrl[urlIndex]
  },
  getHeader(){
    let header = {
      'Content-Type': 'application/json'
    }
    if (com.isbool(this.getCookie())){
      header['access-token'] = this.getCookie()
    }
    return header
  },
  get (url, data, func) {
    let _this = this
    // axios.get(baseUrl + url, {params: data})
    axios({
      method: 'GET',
      url: _this.getUrl() + url,
      params: data,
      headers: _this.getHeader()
    })
    .then(function (res) {
      _this.setCookie(res.headers['access-token'])
      func(res.data)
      if (res.data.s == 0) {
        vue.$toast.error(res.data.i)
        if (res.data.i === '请登录后再操作') {
          vue.$goto('/login')
        }
      }
    })
    .catch(function (error) { // 请求失败处理
      console.log(error)
    })
  },
  post (url, data, func) {
    let _this = this
    axios({
      method: 'POST',
      url: _this.getUrl() + url,
      data: data,
      headers: _this.getHeader()
    })
    .then(function (res, d) {
      _this.setCookie(res.headers['access-token'])
      func(res.data)
      if (res.data.s == 0) {
        vue.$toast.error(res.data.i)
        if (res.data.i === '请登录后再操作') {
          vue.$goto('/login')
        }
      }
    })
    .catch(function (error) { // 请求失败处理
      console.log(error)
    })
  },
  upload ($file, func ,isFile) {
    let _this = this
    var formdata = new FormData()
    formdata.append('Filedata', $file)
    let url = '/MyWebPicUpload'
    if (isFile !== undefined) {
      url = '/MyFileUpload'
    }

    axios({
      url: _this.getUrl() + url,
      method: 'post',
      data: formdata,
      headers: _this.getHeader()
    }).then((res) => {
      // 第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)
      /**
       * $vm 指为mavonEditor实例，可以通过如下两种方式获取
       * 1. 通过引入对象获取: `import {mavonEditor} from ...` 等方式引入后，`$vm`为`mavonEditor`
       * 2. 通过$refs获取: html声明ref : `<mavon-editor ref=md ></mavon-editor>，`$vm`为 `this.$refs.md`
       */
      func(res.data)
    })
  },
  uploadMany ($file, func) {
    var formdata = new FormData()
    formdata.append('Filedata', $file)
    axios({
      url: baseUrl + '/MyMultiPicUpload',
      method: 'post',
      data: formdata,
      headers: {'Content-Type': 'multipart/form-data'},
    }).then((res) => {
      // 第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)
      /**
       * $vm 指为mavonEditor实例，可以通过如下两种方式获取
       * 1. 通过引入对象获取: `import {mavonEditor} from ...` 等方式引入后，`$vm`为`mavonEditor`
       * 2. 通过$refs获取: html声明ref : `<mavon-editor ref=md ></mavon-editor>，`$vm`为 `this.$refs.md`
       */
      func(res.data)
    })
  },
  getCookie () {
    let at = localStorage.getItem('access_token')
    return at
  },
  setCookie (at) {
    localStorage.setItem('access_token', at)
  },
  download (url, data, func) {
    let _this = this
    axios( {
      method: 'POST',
      url: _this.getUrl() + url,
      data: data,
      responseType: 'blob', //二进制流
      headers: _this.getHeader()
    }).then(function (res) {
      func()
      let data =  res.data;
      if (!data) {
        return
      }
      var blob = new Blob([data])
      // var blob = new Blob([data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'})
      var url = window.URL.createObjectURL(blob)
      var aLink = document.createElement('a')
      aLink.style.display = 'none'
      aLink.href = url
      aLink.setAttribute('download', 'download.xls')
      document.body.appendChild(aLink)
      aLink.click()
      document.body.removeChild(aLink) //下载完成移除元素
      window.URL.revokeObjectURL(url) //释放掉blob对象
    }).catch(function (error) {
      console.log(error)
    });
  }
}
